package com.atguigu.gulimall.search.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadTest2 {
    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("start...");
//        CompletableFuture.runAsync(()->{
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//        },executor);
//        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 0;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executor).whenComplete((res,exception)->{
//            //相当于监听器，只能监听到结果和异常不能修改
//            System.out.println("异步任务完成结果是："+res+"异常是："+exception);
//        }).exceptionally(throwable -> {
//            //可以感知异常并修改返回值
//            return 10;
//        });
//        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 0;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executor).handle((res, exception) -> {
//            System.out.println("异步任务完成结果是："+res+"异常是："+exception);
//            if (res != null) {
//                return res * 2;
//            }
//            if (exception != null) {
//                return 20;
//            }
//            return 0;
//        });


//        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executor).thenApplyAsync((in) -> {
//            System.out.println("接受到的返回值为：" + in);
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            System.out.println("经行业务处理");
//            in = 10;
//            return "hello world";
//        },executor);
//        String s = future.get();
//        System.out.println(s);
//        System.out.println("end...");
//
//        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务1开始。。。" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("任务1结束。。。");
//            return i;
//        }, executor);
//
//        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务2开始。。。" + Thread.currentThread().getId());
//            try {
//                Thread.sleep(3000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("任务2结束。。。");
//            return "hello world";
//        }, executor);
//
//        future1.runAfterBothAsync(future2,()->{
//            System.out.println("任务3开始...");
//        },executor);
//
//        future1.thenAcceptBothAsync(future2,(res1,res2)->{
//            System.out.println("任务3开始...");
//            System.out.println("结果1是："+res1);
//            System.out.println("结果2是："+res2);
//        },executor);
//
//        CompletableFuture<String> stringCompletableFuture = future1.thenCombineAsync(future2, (res1, res2) -> {
//            System.out.println("任务3开始...");
//            return res1 + res2;
//        }, executor);
//
//        //阻塞住了
//        System.out.println(stringCompletableFuture.get());
//
//        CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务1开始。。。" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("任务1结束。。。");
//            return i;
//        }, executor);
//
//        CompletableFuture<Object> future2 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务2开始。。。" + Thread.currentThread().getId());
//            try {
//                Thread.sleep(3000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("任务2结束。。。");
//            return "hello world";
//        }, executor);

//        future1.runAfterEitherAsync(future2,()->{
//            System.out.println("任务3开始...");
//        },executor);

//        CompletableFuture<String> stringCompletableFuture = future1.applyToEitherAsync(future2, (res) -> {
//            System.out.println("任务3开始...");
//            return res + "hhhhh";
//        }, executor);
//
//        System.out.println("end...");
//
//        System.out.println(stringCompletableFuture.get());

        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品图片信息");
            return "hello.jpg";
        },executor);

        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品属性");
            return "黑色+256G";
        },executor);

        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("查询商品介绍");
            return "华为";
        },executor);

        CompletableFuture<Object> objectCompletableFuture = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
        Object o = objectCompletableFuture.get();
        System.out.println(o);

//        CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
//        //阻塞
//        allOf.get();

        System.out.println("end...");
    }
}
